import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jpf_core/util/colors_util.dart';

class JPFButton extends StatefulWidget{
  String _text;
  Function _onBtnClick;
  double _height;
  double _width;
  EdgeInsets _margin;
  Color _lineColor;
  double _radius;
  List<Color> _bgColors;
  Color _textColor;
  double _textSize;
  bool _enabled;
  static List<Color> btnColors = [ColorsUtil.blue_dark1, ColorsUtil.blue_dark2];

  JPFButton(String text, Function onBtnClick, {
    double height = 40,
    double width,
    EdgeInsets margin,
    Color lineColor = Colors.transparent,
    double radius = 5,
    List<Color> bgColors,
    Color textColor = Colors.white,
    double textSize = 15,
    bool isEnabled = true,
  }){
    _text = text;
    _onBtnClick = onBtnClick;
    _width = width;
    _height = height;
    _margin = margin;
    _lineColor = lineColor;
    _radius = radius;
    if(bgColors == null ){
      _bgColors = btnColors;
    }else if(bgColors.length>1){
      _bgColors = bgColors;
    }else{
      _bgColors = [bgColors[0],bgColors[0]];
    }
    _textColor = textColor;
    _textSize = textSize;
    _enabled = isEnabled;
  }

  JPFButtonState _btnState;

  @override
  State<StatefulWidget> createState() {
    _btnState = JPFButtonState();
    return _btnState;
  }
}

class JPFButtonState extends State<JPFButton>{
  bool _isPressDown = false;

  @override
  Widget build(BuildContext context) {

    return GestureDetector(
      child: Container(
        width: widget._width,
        height: widget._height,
        margin: widget._margin,
        child: new Text(widget._text, style: TextStyle(
            fontSize: widget._textSize,
            color: widget._enabled ? widget._textColor : Colors.white60)),
        alignment: Alignment.center,
        decoration:_getDecoration(),
      ),
      onTap: widget._enabled ? widget._onBtnClick : null,
      onTapDown: (downDetail){
        setState(() {
          _isPressDown = true;
        });
      },
      onTapUp: (upDetail){
        setState(() {
          _isPressDown = false;
        });
      },
      onTapCancel: (){
        setState(() {
          _isPressDown = false;
        });
      },
    );
  }

  BoxDecoration _getDecoration(){
    if(!widget._enabled){
      return _getDisEnableBoxDecoration();
    }else{
      if(_isPressDown){
        return _getPressedBoxDecoration();
      }else{
        return _getDefaultBoxDecoration();
      }
    }
  }

  BoxDecoration _getDefaultBoxDecoration(){
    return BoxDecoration(
      border: Border.all(color: widget._lineColor, width: 1.0),
      borderRadius: BorderRadius.circular(widget._radius),
      gradient: LinearGradient(colors: widget._bgColors),
    );
  }

  BoxDecoration _getPressedBoxDecoration(){
    return BoxDecoration(
      border: Border.all(color: widget._lineColor, width: 1.0),
      borderRadius: BorderRadius.circular(widget._radius),
      color: widget._bgColors[0],
    );
  }

  BoxDecoration _getDisEnableBoxDecoration(){
    return BoxDecoration(
      border: Border.all(color: widget._lineColor, width: 1.0),
      borderRadius: BorderRadius.circular(widget._radius),
      color: Color(0xffcccccc),
    );
  }
}